﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using ScrumTable.UI.View.Util;

#endregion

namespace ScrumTable.UI.View.Util
{

    /// <summary>
    /// DropDropUtil Util
    /// </summary>
    public static class BindingUtil
    { 

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
 
        public static void BindElementPath(Control control, DependencyProperty property ,string path, string elementName)
        {
            var bind = new Binding { Path = new PropertyPath(path), ElementName = elementName };
            control.SetBinding(property, bind);
        }

        public static void BindSourcePath(Control control, DependencyProperty property ,string path, object model)
        {
            BindSourcePath(control, property, path, model, BindingMode.TwoWay);
        }

        public static void BindSourcePath(Control control, DependencyProperty property, string path, object model, BindingMode mode)
        {
            var bind = new Binding { Path = new PropertyPath(path), Source = model, Mode = mode };
            control.SetBinding(property, bind);
        }

        public static void BindSourcePath(Control control, DependencyProperty property, string path, object model, IValueConverter converter, ValidationRule rule = null, BindingMode mode = BindingMode.TwoWay)
        {
            var bind = CreateBindSourcePath(path, model, converter, mode);
            if (rule != null)
            {
                bind.ValidationRules.Add(rule);
            }
            control.SetBinding(property, bind);
        }

        public static Binding CreateBindSourcePath(string path, object model, IValueConverter converter, BindingMode mode = BindingMode.TwoWay)
        {
            return new Binding {Path = new PropertyPath(path), Source = model, Mode = mode, Converter = converter};
        }

        #endregion
    }
}
